package rak.r91.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import rak.r91.data.Enterprise;
import rak.r91.data.Person;

public class JdbcSearchDao extends JdbcDaoSupport implements SearchDao{

	@Override
	public List<Person> personSearch(String condition) {
		return getJdbcTemplate().query("SELECT person, first_name, last_name, identity_code FROM person WHERE upper(first_name) LIKE upper('"+condition+"%')", new personMapper());
	}
	
	private static final class personMapper implements RowMapper<Person> {
	    public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
	        Person person = new Person();
	        person.setPersonID(rs.getInt("person"));
	        person.setFirstName(rs.getString("first_name"));
	        person.setLastName(rs.getString("last_name"));
	        person.setIdentityCode(rs.getString("identityCode"));
	        return person;
	    }
	}

	@Override
	public List<Enterprise> enterpriseSearch(String condition) {
		return getJdbcTemplate().query("SELECT person, first_name, last_name, identity_code FROM person WHERE upper(first_name) LIKE upper('"+condition+"%')", new enterpriseMapper());
	}
	
	private static final class enterpriseMapper implements RowMapper<Enterprise> {
	    public Enterprise mapRow(ResultSet rs, int rowNum) throws SQLException {
	        Enterprise enterprise = new Enterprise();
	        enterprise.setEnterpriseID(rs.getInt("enterprise"));
	        enterprise.setName(rs.getString("name"));
	        return enterprise;
	    }
	}
	

}
